{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "## 基础知识\n",
    "\n",
    "### tensor 张量作为基本单元，等同于多为数组，标量（0维数组）向量（1维数组）、矩阵（2维数组）\n",
    "### 知识点\n",
    "\n",
    "\n",
    "    1. tf.square():对张量的每一个元素求平方，不改变张良的形状\n",
    "    2.tf.reduce_sum()对输入张量的所有元素求和，可以通过axis参数来指定求和的维度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import tensorflow as tf"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "#定义一个随机数\n",
    "random_float=tf.random.uniform(shape=())\n",
    "#定义一个有两个元素的零向量\n",
    "zero_vector=tf.zeros(shape=(2))\n",
    "#定义两个2x2的常量矩阵\n",
    "A=tf.constant([[1.,2.],[3.,4.]])\n",
    "B=tf.constant([[5.,6.],[7.,8.]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 通过shpe、dtype、numpy获得张量的形状、类型、值\n",
    "print(A.shape)\n",
    "print(A.dtype)\n",
    "print(A.numpy)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 通过tf.GradientTpye()  求导记录器   实现自动求导\n",
    "\n",
    "\n",
    "    tf.GradientTape() 是一个自动求导的记录器。只要进入了 with tf.GradientTape() as tape 的上下文环境，则在该环境中计算步骤都会被自动记录。比如在上面的示例中，计算步骤 y = tf.square(x) 即被自动记录。离开上下文环境后，记录将停止，但记录器 tape 依然可用，因此可以通过 y_grad = tape.gradient(y, x) 求张量 y 对变量 x 的导数。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "x=tf.constant([[1.,2.],[3.,4.]])\n",
    "y=tf.constant([[1.],[2.]])\n",
    "w=tf.Variable(initial_value=[[1.],[2.]])\n",
    "b=tf.Variable(initial_value=1.)\n",
    "with tf.GradientTape() as tape:\n",
    "    l=tf.reduce_sum(tf.square(tf.matmul(x,w)+b-y))\n",
    "w_grad,b_grad=tape.gradient(l,[w,b])\n",
    "print(l,w_grad,b_grad)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### python 知识点\n",
    "    zip()函数：\n",
    "        将可迭代的对象作为参数，将对象中对应元素打包成一个个元组，然后返回有这些元祖组成的list\n",
    "        \n",
    "        \n",
    "        \n",
    "      python 使用super（）函数调用父类方法，使用__call__（）方法对实例进行调用\n",
    "      装饰器"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
